package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * projectName: wokepase1
 *
 * @author: 强力
 * time: 2021/9/14 16:12
 * description:
 */
@Service
public class StroageServiceImpl implements StroageService{

    @Autowired
    private DtsStorageMapper storageMapper;
    //服务器地址
    @Value("${aliyun.oss.endpoint}")
    private  String endpoint;
    //子账户名称
    @Value("${aliyun.oss.accessKeyId}")
    private  String accessKeyId;
    //子账户秘钥
    @Value("${aliyun.oss.accessKeySecret}")
    private  String accessKeySecret;
    //桶的名字
    @Value("${aliyun.oss.bucketName}")
    private  String bucketName;


    @Override
    public IPage<DtsStorage> findBypage(Integer page, Integer limit, String key, String name, String sort, String order) {
        //第一参数当前页,第二参数每页条数
        Page<DtsStorage> storagePage = new Page<>(page, limit);


        QueryWrapper<DtsStorage> queryWrapper = new QueryWrapper<DtsStorage>();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.like("`key`", key);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name", name);
    }
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                queryWrapper.orderByDesc(sort);
            }
        Page<DtsStorage> storagelist = storageMapper.selectPage(storagePage, queryWrapper);
        return storagelist;
    }

    @Override
    public DtsStorage insert(String name, String ext, Integer size, byte[] content) throws Exception {


        //将文件上传到阿里云oss中存储
        //使用随机字符串生成文件的key
        String key = UUID.randomUUID().toString();
        //将文件存储到阿里云中
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName, key + ext, content);

        //将文件保存路径存储到mysql数据库的对应表中
        DtsStorage storage = new DtsStorage();
        //文件上传路径
        storage.setUrl(url);
        //更新时间
        storage.setUpdateTime(new Date());
        //添加时间
        storage.setAddTime(new Date());
        //文件大小
        storage.setSize(size);
        //文件类型, 扩展名
        storage.setType(ext);
        //文件名
        storage.setName(name);
        //文件的key, 也就是文件的唯一标识, key + ext
        storage.setKey(key + ext);
        storage.setDeleted(false);

        storageMapper.insert(storage);

        return storage;
    }

    @Override
    public void delectByKey(String key) {
        QueryWrapper<DtsStorage> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("`key`",key);

        storageMapper.delete(queryWrapper);
    }

    @Override
    public int updateFile(DtsStorage storage) {
        storage.setUpdateTime(new Date());
        int count = storageMapper.updateById(storage);
        return count;

    }


}
